package day1220_test;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    Socket s=null;
    Client() throws IOException {
        s=new Socket("localhost",8088);
    }

    public static void main(String[] args) throws IOException {
        Client   c=new Client();//1.1构造ip端口连接服务端，输入流发送
        c.start();//1.2.输入流发送     //2.线程读取
    }
    class ReadMsgFromServer extends  Thread{
        public void run() {
            try {
                BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream(),"UTF-8"));//run： 用socket获取输入流读取服务端返回的数据
                String msgFromServer=null;
                while( (msgFromServer=br.readLine())!=null ){
                    System.out.println("来自服务器返回的数据："+ msgFromServer);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public void start() throws IOException {
        //2.开启线程： 读取服务端返回的数据
        ReadMsgFromServer  rmfs=new ReadMsgFromServer();
        rmfs.start();

        //1.2.输入流发送数据
        PrintWriter pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())));
        Scanner s=new Scanner(System.in);
        while(true){
            System.out.println("请输入您要发送的数据：");
            String msg=s.nextLine();
            pw.println(msg);
            pw.flush();
        }
    }







}